from flask import request, jsonify
from wtforms import Form


class ApiBaseForm(Form):
    def __init__(self, *args, **kwargs):
        data = request.get_json(silent=True)
        args = request.args.to_dict()
        super(ApiBaseForm, self).__init__(data=data, *args, **kwargs)

    def validate_for_api(self):
        valid = super(ApiBaseForm, self).validate()
        if not valid:
            # form errors
            raise jsonify(dict(msg=self.errors, status=400))
        return self
